package label.service.impl;


import article.entity.ArticleVO;
import articleLabel.dao.ArticleLabelDao;
import articleLabel.entity.ArticleLabelVO;
import blogData.entity.BlogDataVO;
import generic.page.Pages;
import label.dao.LabelDao;
import label.entity.LabelVO;
import label.service.LabelService;

import java.util.ArrayList;
import java.util.List;

public class LabelServiceImpl implements LabelService {

    private LabelDao labelDao;

    private ArticleLabelDao articleLabelDao;

    public void setArticleLabelDao(ArticleLabelDao articleLabelDao) {
        this.articleLabelDao = articleLabelDao;
    }

    public void setLabelDao(LabelDao labelDao) {
        this.labelDao = labelDao;
    }

    @Override
    public LabelVO findLabelVOById(Integer labelId) {
        return this.labelDao.findLabelVOById(labelId);
    }

    @Override
    public List<LabelVO> findLabelVOByIds(Integer[] labelIds, List<LabelVO> labelVOs) {
        labelVOs=new ArrayList<LabelVO>();
        for(int i=0;i<labelIds.length;i++){
            labelVOs.add(this.findLabelVOById(labelIds[i]));
        }
        return labelVOs;
    }

    @Override
    public void deleteLabelById(Integer labelId) {
        this.labelDao.deleteLabelById(labelId);
    }

    @Override
    public LabelVO updateLabel(LabelVO labelVO) {
        return this.labelDao.updateLabel(labelVO);
    }

    @Override
    public List<LabelVO> addLabelVO(BlogDataVO blogDataVO, List<LabelVO> labelVOs, ArticleVO articleVO) {
        for (int i = 0; i < labelVOs.size(); i++) {
            LabelVO labelVO = labelVOs.get(i);
            if (!("").equals(labelVO.getLabelName()) && labelVO != null) {

                LabelVO labelVOE = labelDao.existsOdLabel(labelVO, blogDataVO.getBlogDataId());
                if (labelVOE == null) {
                    blogDataVO.getLabelVOs().add(labelVO);

                    ArticleLabelVO al = new ArticleLabelVO(articleVO, labelVO);

                    labelVO.getArticleLabelVOs().add(al);

                    articleVO.getArticleLabelVOs().add(al);

                    labelVO.setBlogDataVO(blogDataVO);

                    this.labelDao.addLabelVO(labelVO);
                }
            }
        }
        return labelVOs;
    }


    @Override
    public Pages queryForPage(Pages pages, Integer blogDataId) {
        if (pages == null) {
            pages = new Pages();
        }
        return this.labelDao.queryPageByBlogData(pages, blogDataId);
    }

    @Override
    public List<LabelVO> findAllLabelByBlogData(Integer blogDataId) {
        return this.labelDao.findAllLabelByBlogData(blogDataId);
    }

    @Override
    public List<LabelVO> findLabelByBlogData(List<LabelVO> labelVOs, Integer blogDataId) {
        List<LabelVO> labelVOsByBlogData=null ;
        for (int i = 0; i < labelVOs.size(); i++) {
            LabelVO labelVO = labelVOs.get(i);
            if (!("").equals(labelVO.getLabelName()) && labelVO != null) {

                LabelVO labelVOByBlogData = labelDao.existsOdLabel(labelVO, blogDataId);
                labelVOsByBlogData.add(labelVOByBlogData);
            }
        }
        return labelVOsByBlogData;
    }

        @Override
        public LabelVO addLabelByBlogData (LabelVO labelVO, BlogDataVO blogDataVO){
            blogDataVO.getLabelVOs().add(labelVO);
            labelVO.setBlogDataVO(blogDataVO);
            return this.labelDao.addLabelVO(labelVO);
        }
    }
